import {defineStore} from 'pinia';
import {
    getRecentBorrowingRecords, getStats, borrowAsset, getUserBorrowList, returnAsset
} from '@/api/user/borrow.js';
import {ElMessage} from "element-plus";

export const useBorrowStore = defineStore('borrow', {
    state: () => ({
        recentBorrowingRecords: [],
        totalAssets: 0,
        availableAssets: 0,
        borrowedAssets: 0,
        loading: false,
        userBorrowList: [], // 用户借用列表
        page: 1, // 当前页码
        pageSize: 10, // 每页显示数量
        total: 0, // 总记录数
    }), getters: {}, actions: {
        // 获取最近借用记录
        async fetchRecentBorrowingRecords() {
            this.loading = true;
            try {
                const response = await getRecentBorrowingRecords();
                if (response.code === 200 && response.data) {
                    this.recentBorrowingRecords = response.data;
                } else {
                    console.error('获取最近借用记录失败:', response.message);
                    ElMessage.error({message: response.message || "获取最近借用记录数据失败", duration: 3000});
                }
            } catch (error) {
                console.error('Error fetching recent borrowing records:', error);
                ElMessage.error({message: "获取最近借用记录数据失败", duration: 3000});
            } finally {
                this.loading = false;
            }
        }, // 获取统计数据
        async fetchStats() {
            this.loading = true;
            try {
                const response = await getStats();
                this.totalAssets = response.data.totalAssets;
                this.availableAssets = response.data.availableAssets;
                this.borrowedAssets = response.data.borrowedAssets;
            } catch (error) {
                console.error('Error fetching stats:', error);
                ElMessage.error({message: "统计失败", duration: 3000});
            } finally {
                this.loading = false;
            }
        }, // 借用资产
        async borrowAsset(borrowForm) {
            try {
                const response = await borrowAsset(borrowForm);
                if (response.code === 200) {
                    return true;
                } else {
                    console.error(response.message || '借用资产失败');
                    ElMessage.error({message: response.message || "借用资产失败", duration: 3000});
                    return false;
                }
            } catch (error) {
                console.error(error.message || '借用资产失败');
                ElMessage.error({message: error.message || "借用资产失败", duration: 3000});
                return false;
            }
        }, // 归还资产
        async returnAsset(borrowId) {
            try {
                const response = await returnAsset(borrowId);
                if (response.code === 200) {
                    return true;
                } else {
                    console.error(response.message || '归还资产失败');
                    ElMessage.error({message: response.message || "归还资产失败", duration: 3000});
                    return false;
                }
            } catch (error) {
                console.error(error.message || '归还资产失败');
                ElMessage.error({message: error.message || "归还资产失败", duration: 3000});
                return false;
            }
        }, // 获取用户借用记录
        async getUserBorrowList(params) {
            this.loading = true;
            try {
                const response = await getUserBorrowList(params);
                if (response.code === 200) {
                    this.userBorrowList = response.data.records;
                    this.total = response.data.total;
                } else {
                    console.error(response.message || '获取借用记录失败');
                    ElMessage.error({message: response.message || "获取借用记录失败", duration: 3000});
                }
            } catch (error) {
                console.error(error.message || '获取借用记录失败');
                ElMessage.error({message: error.message || "获取借用记录失败", duration: 3000});
            } finally {
                this.loading = false;
            }
        }, // 设置当前页码
        setPage(page) {
            this.page = page;
        }, // 设置每页显示记录数
        setPageSize(pageSize) {
            this.pageSize = pageSize;
        },
    },
});